package com.beverage.lemonade;

import org.json.JSONException;

import com.beverage.lemonade.UI.BaseActivity;
import com.beverage.lemonade.process.Constants;
import com.beverage.lemonade.process.CreateTask;
import com.beverage.lemonade.process.LogInTask;
import com.beverage.lemonade.process.SignInTask;
import com.beverage.lemonade.process.UserInfoJson;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.SpannableStringBuilder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BackUpActivity extends BaseActivity {

	private Button confirm;
	private EditText username;
	private EditText password;
	private TextView usernameT;

	private Context context;
	private SharedPreferences sp;
	private CreateTask createTask;
	private SignInTask signInTask;

	private OnClickListener confirmAction = new OnClickListener() {

		public void onClick(View arg0) {

			SpannableStringBuilder sb = (SpannableStringBuilder) username
					.getText();
			String usernameS = sb.toString();
			sb = (SpannableStringBuilder) password.getText();
			String passwordS = sb.toString();

			if (!sp.getString(Constants.USERNAME, "").equals("")) {

				UserInfoJson userInfoJson = null;
				try {
					userInfoJson = new UserInfoJson(context);
					userInfoJson.setUsername(usernameS);
					userInfoJson.setPassword(passwordS);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				signInTask = new SignInTask(context);
				signInTask.execute(userInfoJson);

			} else {
				UserInfoJson userInfoJson = null;
				try {
					userInfoJson = new UserInfoJson(context);
					userInfoJson.setUsername(usernameS);
					userInfoJson.setPassword(passwordS);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				createTask = new CreateTask(context);
				createTask.execute(userInfoJson);
				sp.edit().putInt(Constants.ISBACKUP, 1);

			}

		}

	};

	public BackUpActivity() {
		super(R.string.title_activity_back_up);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;

		sp = PreferenceManager.getDefaultSharedPreferences(this);
		if (sp.getInt(Constants.ISBACKUP, 0) == 0) {
			setUnBackUp();
		} else {
			setBackUp();
		}

	}

	private void setUnBackUp() {
		setContentView(R.layout.activity_back_up);
		confirm = (Button) findViewById(R.id.confirm);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);

		confirm.setOnClickListener(confirmAction);
	}

	private void setBackUp() {
		setContentView(R.layout.activity_back_up_true);
		usernameT = (TextView) findViewById(R.id.username);
		usernameT.setText(sp.getString(Constants.USERNAME, "username"));

	}

}
